package com.tkjt.pms.calc.loader.impl;

import java.util.List;

import com.tkjt.pms.common.entity.TkDepartGroup;
import com.tkjt.pms.modules.common.mapper.TkDepartGroupMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.calc.common.IndexCalcParallel;
import com.tkjt.pms.calc.formula.common.FormulaRule;
import com.tkjt.pms.calc.formula.common.FormulaRules;
import com.tkjt.pms.calc.loader.FormulaRuleLoader;
import com.tkjt.pms.calc.utils.EsCalcUtils;
import com.tkjt.pms.common.utils.EsJsonUtils;
import com.tkjt.pms.modules.tkindexvaluerule.entity.TkIndexValueRule;
import com.tkjt.pms.modules.tkindexvaluerule.mapper.TkIndexValueRuleMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class FormulaRuleLoaderImpl implements FormulaRuleLoader {

	@Autowired
	TkIndexValueRuleMapper tkIndexValueRuleMapper;
	@Autowired
	TkDepartGroupMapper tkDepartGroupMapper;
	
	@Override
	public FormulaRules doLoader(IndexCalcParallel indexCalcParallel) {
		
		FormulaRules formulaRules = new FormulaRules();
		
		log.info("load formula rules, indexCalcParallel={}", indexCalcParallel);

		QueryWrapper<TkIndexValueRule> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkIndexValueRule::getIndexValueCode, indexCalcParallel.getIndexValueCode());
		List<TkIndexValueRule> tkIndexValueRules = tkIndexValueRuleMapper.selectList(queryWrapper);
		for (TkIndexValueRule rule : tkIndexValueRules) {
			String ruleIndexValueCode = rule.getIndexValueCode();
			String ruleWhichData = rule.getWhichData();
			String ruleIndexDepart = rule.getIndexDepart();
			if(StringUtils.isNotEmpty(ruleIndexDepart) && (ruleIndexDepart.startsWith("ATRD") || ruleIndexDepart.startsWith("ATRT"))){
				QueryWrapper<TkDepartGroup> fepartGroupQueryWrapper = new QueryWrapper<>();
				fepartGroupQueryWrapper.lambda().eq(TkDepartGroup::getDepartBiz,ruleIndexDepart);
				List<TkDepartGroup> departGroups = tkDepartGroupMapper.selectList(fepartGroupQueryWrapper);
				for(TkDepartGroup tkDepartGroup : departGroups){
					String ruleKey = EsCalcUtils.buildRuleKey(ruleIndexValueCode, ruleWhichData, tkDepartGroup.getIndexDepart());
					FormulaRule formulaRule = new FormulaRule();
					formulaRule.setFormula(EsCalcUtils.formatValueRule(rule.getIndexValueRule()));
					formulaRule.setFormulaCN(EsCalcUtils.formatValueRule(rule.getIndexValueRuleCn()));
					formulaRules.addFormula(ruleKey, formulaRule);
				}
			}else{
				String ruleKey = EsCalcUtils.buildRuleKey(ruleIndexValueCode, ruleWhichData, ruleIndexDepart);
				FormulaRule formulaRule = new FormulaRule();
				formulaRule.setFormula(EsCalcUtils.formatValueRule(rule.getIndexValueRule()));
				formulaRule.setFormulaCN(EsCalcUtils.formatValueRule(rule.getIndexValueRuleCn()));
				formulaRules.addFormula(ruleKey, formulaRule);
			}

		}
		
		log.info("load end, formulaRules: \r\n{}", EsJsonUtils.toJsonStr(formulaRules));
		
		return formulaRules;
	}

}
